package com.jian.dao.impl;

import com.jian.dao.UserDAO;
import com.jian.pojo.User;
import com.jian.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author Jiange
 * @create 2021-04-06-20:07
 */
public class UserDaoImpl extends BaseDAO implements UserDAO {
    @Override
    public User getUser(String username) throws SQLException {
        String sql = "select username,password,email,id from user where username=?";
        Connection connection = JDBCUtils.getConnection();
        User user = queryForOne(connection, sql, User.class, username);
        JDBCUtils.closeConnection(connection);
        return user;
    }

    @Override
    public int saveUsername(User user) throws SQLException {
        String sql = "insert into user(username,password,email) values(?,?,?)";
        Connection connection = JDBCUtils.getConnection();
        int update = update(connection, sql, user.getUsername(), user.getPassword(), user.getEmail());
        JDBCUtils.closeConnection(connection);
        return update;
    }

    @Override
    public User queryUsernameAndPassword(String username, String password) throws SQLException {
        String sql = "select username,password,email,id from user where username=? and password=?";
        Connection connection = JDBCUtils.getConnection();
        User user = queryForOne(connection, sql, User.class, username,password);
        return user;
    }
}
